home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / N-P / OOP for C.cpt / OIC.ƒ / list2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-09  |  2.1 KB  |  112 lines  |  [TEXT/KAHL]

  1. /*    
  2.  *        List abstract class - alternate kind using Collect as SuperClass
  3.  *
  4.  *                        Andrew Nicholson - Not-So-Soft, Oz.
  5.  *
  6.  *    Superclasses:
  7.  *
  8.  *        Collect
  9.  *
  10.  *    methods:
  11.  *
  12.  *    head l        - returns first item in list l
  13.  *    tail l        - returns list l minus 1st item
  14.  *    push l item - pushes item on front of l
  15.  *    isEmpty l    - returns non-zero if list is empty, ,zero if not
  16.  *    eq l1 l2    - test isomorphic equality between l1 & l2
  17.  *
  18.  */
  19.  
  20. #include "oic.h"
  21. #include "generics.h"
  22.  
  23. class      List2;            
  24.  
  25. typedef struct                    /* list instance structure                 */
  26. {
  27.     object    l_next;
  28.     object    l_item;
  29. } list_i;
  30.  
  31. /* -------------------- List Instance methods ---------------------------- */
  32.  
  33. static object
  34. _head(self, list)                /* return first item in the list         */
  35.     object            self;
  36.     register list_i *list;
  37. {
  38.     return list->l_item;
  39. }
  40.  
  41. static object
  42. _tail(self, list)                /* return list following the head */
  43.     object             self;
  44.     register list_i    *list;
  45. {
  46.     return (list->l_item == END) ? self : list->l_next;
  47. }
  48.  
  49. static object
  50. _push(self, list, item)        /* put the item at the start of list */
  51.     object            self;
  52.     register list_i    *list;
  53.     object            *item;
  54. {
  55.     register object    newl;
  56.     register list_i *newinst;
  57.  
  58.     newl = New(List2);
  59.     newinst = localIVs(newl, list_i);
  60.     newinst->l_next = list->l_next;
  61.     newinst->l_item = list->l_item;
  62.     list->l_next = newl;
  63.     list->l_item = *item;
  64.  
  65.     return self;
  66. }
  67.  
  68. static int
  69. _isEmpty(self, list)            /* does list have no items in it */
  70.     object            self;
  71.     register list_i *list;
  72. {
  73.     return (list->l_item == END);
  74. }
  75.  
  76. static int
  77. _eq(self, list, otherlist)
  78.     object    self;
  79.     list_i    *list;
  80.     object    *otherlist;
  81. {
  82.     register object    head1, head2;
  83.  
  84.     head1 = head(self);
  85.     head2 = head(*otherlist);
  86.     
  87.     if (head1 == END && head2 == END)    /* This should use "isEmpty" */
  88.         return 1;
  89.     if (head1 == END || head2 == END)
  90.         return 0;
  91.     if ((int)eq(head1, head2))
  92.         return ((int)eq(tail(self), tail(*otherlist)));
  93.     else
  94.         return 0;
  95. }
  96.  
  97. /* ------------------- Init the List class ------------------------------- */
  98.  
  99. _InitList2()
  100. {
  101.     List2 = NewClass(sizeof(list_i), 0, "List2", Collect, END);
  102.     AddMethods(List2, 
  103.         headGeneric,        _head,
  104.         tailGeneric,        _tail,
  105.         pushGeneric,        _push,
  106.         isEmptyGeneric,        _isEmpty,
  107.         eqGeneric,            _eq,
  108.         END
  109.        );
  110. }
  111.  
  112.